//@see next js function is for org tree init add edit delete 8:58 2008-2-26

/**
* @todo：组织树初始化
* @date：2008-7-31 18:02
* @author：hyh
*/
function ajaxTreeInit(){
	loadTreeParentNode();
	initTree();//初始化树
	initContextMenu();//初始化菜单
}
//读取父节点(固定方法)
function loadTreeParentNode(){
	DWRUtil.useLoadingMessage("正在读取数据...");
	DWREngine.setAsync(false);//设置同步
	imageFolder = "../components/folder-tree-static/images/";
	//父节点
	var html = "<ul id=\"ajax_tree\" class=\"dhtmlgoodies_tree\">";
	ConfigAction.getAclorganizationById("0",function(data){
		//配置A，id 提供修改时不用重新读取数据库
		html += "<li><a href=\"javascript:void(0);\" id=\""+  data.id+ "\" onclick=\"getDetailTreeNode('"+ data.id +"');return false;\">"+ data.deptname +"</a><ul><li parentId=\""+ data.id +"\" class=\"loading.gif\"><a href=\"javascript:void(0);\">Loading...</a></li></ul></li>";
	});
	html += "</ul>";
	$("org_tree").innerHTML = html;
	DWREngine.setAsync(true);//设置异步
}
//读取子节点(固定方法)
function loadTreeChildNodes(ulId,parentId){
	DWRUtil.useLoadingMessage("正在读取数据...");
	var html = "";
	ConfigAction.listAclorganizationByParentId(parentId,function(data){
		if(data != null && data.length >0){
			for(var i = 0;i< data.length;i++){
				html += "<li><a href=\"javascript:void(0);\" id=\""+  data[i].id+ "\" onclick=\"getDetailTreeNode('"+ data[i].id +"');return false;\">"+ data[i].deptname+ "</a><ul><li parentId=\""+ data[i].id +"\" class=\"loading.gif\"><a href=\"javascript:void(0);\">Loading</a></li></ul></li>";
			}
		}
		$(ulId).innerHTML = html;
		if(html == ""){
			$(ulId).parentNode.getElementsByTagName('IMG')[0].style.visibility = "hidden";
			$(ulId).style.display="none";
		}
		parseSubItems(ulId,parentId);
	});
}

/**
* @todo：添加组织树节点(固定方法)
* @date：2008-8-1 9:52
* @author：hyh
*/
function addTreeNode(pId,pTitle){
	orgOpInit();
	DWRUtil.setValue("parentname",pTitle);
	DWRUtil.setValue("parentid",pId);
}

/**
* @todo：编辑组织树节点(固定方法)
* @date：2008-8-1 9:52
* @author：hyh
*/
function editTreeNode(id,pTitle){
	if(id == "0"){
		alert("根节点不能编辑！");
		return;
	}
	orgOpInit(id);
	DWRUtil.setValue("parentname",pTitle);
	setOrgEditHtml();
}

/**
* @todo：组织添加编辑详情初始化
* @date：15:35 2008-2-25
* @author：hyh
*/
function orgOpInit(id){
	setOrgdetail($("div_org_op").innerHTML);
	if(id != null && id != ""){//for edit detail
		setOrgDetailHtml();//detail
		geOrgDetail(id);
	}
}

/**
* @todo：读取组织详情
* @date：15:35 2008-2-25
* @author：hyh
*/
function geOrgDetail(id){
	DWRUtil.useLoadingMessage("正在读取数据...");
	ConfigAction.getAclorganizationById(id,function(oJSOrgBean){
		setFormValue(oJSOrgBean);
	});
}

/**
* @todo：组织保存（添加或编辑）
* @date：16:25 2008-2-25
* @author：hyh
*/
function saveOrg(){
	DWRUtil.useLoadingMessage("正在保存数据...");
	var oJSOrgBean = new JSOrgBean();
	getFormValue(oJSOrgBean);
	if(oJSOrgBean.deptname == ""){
		alert("组织名称不能为空！");
		$("deptname").focus();
		return false;
	}
	if(oJSOrgBean.depttype == "0"){
		alert("请选择组织类型！");
		$("depttype").focus();
		return false;
	}
	if(oJSOrgBean.deptorder == ""){
		alert("组织次序不能为空！");
		$("deptorder").focus();
		return false;
	}
	if(!/^[-]{0,1}[0-9]{1,}$/.test(oJSOrgBean.deptorder)){
		alert("组织次序必须为数字！");
		$("deptorder").focus();
		return false;
	}
	DWREngine.setAsync(false);
	if(!checkDeptcode(oJSOrgBean)){
		alert("此组织编码已经存在！");
		$("deptcode").focus();
		return false;
	}
	if(oJSOrgBean.id == ""){//新增
		ConfigAction.insertAclorganization(oJSOrgBean,function(data){
			alert(data);
			setOrgdetail("");
			loadNodeByParentId(oJSOrgBean.parentid);//重新读取父节点
		});
	}else{//编辑
		ConfigAction.updateAclorganizatio(oJSOrgBean,function(data){
			loadNodeById(oJSOrgBean.id,oJSOrgBean.deptname);//同时修改树的名称
			alert(data);
			setOrgDetailHtml();
		});
	}
	DWREngine.setAsync(true);
	//showBeanValue(oJSOrgBean);
}

/**
* @todo：校验组织编码唯一性
* @date：2008-12-22 
* @author：wangchen
*/
function checkDeptcode(oJSOrgBean){
	var result = true;
	ConfigAction.checkDeptcode(oJSOrgBean,function(data){
		result = data;
	});
	return result;
}

/**
* @todo：删除组织(固定方法)
* @date：2008-8-1 9:53
* @author：hyh
*/
function deleteTreeNode(id){
	DWRUtil.useLoadingMessage("正在删除组织数据...");
	ConfigAction.deleteAclorganization(id,function(data){
		alert(data);
		setOrgdetail("");
	});
}

//@see next js function is for org relation user 8:58 2008-2-26

/**
* @todo：查看组织树用户列表(固定方法)
* @date：2008-8-1 9:53
* @author：hyh
*/
function getDetailTreeNode(id){
	DWRUtil.useLoadingMessage("正在读取数据...");
	if(id == "0"){
		//alert("根节点没有用户！");
		setOrgdetail("");
		return;
	}
	setOrgdetail($("div_orgrelauser_hidden").innerHTML);
	DWRUtil.setValue("orgid",id);
	loadAcluserRelaAclorgList();//load orguser
	ConfigAction.getAclorganizationById(id,function(oJSOrgBean){
		var html = "组织名称:<font color=\"red\"><u>"+ oJSOrgBean.deptname + "</u>" +
		"</font>&nbsp;&nbsp;组织编码:<font color=\"red\"><u>" + oJSOrgBean.deptcode + "</u></font>&nbsp;&nbsp;组织类型:<font color=\"red\"><u>";
		if(oJSOrgBean.depttype == "1"){
			html += "本地网";
		}else if(oJSOrgBean.depttype == "2"){
			html += "下级县市";
		}else if(oJSOrgBean.depttype == "3"){
			html += "普通部门";
		}
		$("zzxi").innerHTML = html+"</u></font>";
		DWRUtil.setValue("orgname",oJSOrgBean.deptname);
	});
}

/**
* @todo：查询部门用户
* @date：9:02 2008-2-26
* @author：hyh
*/
function loadAcluserRelaAclorgList(page,pageSize){
	if(typeof(page) == "undefined"){
		page = 1;
	}
	if(typeof(pageSize) == "undefined"){
		pageSize = 10;
	}
	var oJSOrgRelaUserSearchBean = new JSOrgRelaUserSearchBean();
	getFormValue(oJSOrgRelaUserSearchBean);
	DWRUtil.useLoadingMessage("正在读取数据...");
	ConfigAction.listAcluserByOrgId(oJSOrgRelaUserSearchBean,page,pageSize,function(pageObject){
		  var data = pageObject.resultList;
		  var i = 1;
		  DWRUtil.removeAllRows("orguser_table");
		  var cellFuncs = [
		  function(data) {
			  return "<input type=\"checkbox\" name=\"orguserchkbox\" value=\""+data.userid+"\" class=\"checkbox\">";
		  },
		  function(data) {//用户帐号
			  if(data.account == "null" || data.account == null){
				  return "";
			  }else{
				  return data.account;
			  }
		  },
		  function(data) {//用户名称
			  if(data.realname == "null" || data.realname == null){
				  return "";
			  }else{
				  return data.realname;
			  }	  
		  },
		  function(data) {//用户状态
			  if(data.userstatus == "null" || data.userstatus == null){
				  return "";
			  }else if(data.userstatus == "1"){
			  	  return "激活";
			  }else{
			  	  return "未激活";
			  }
		  },
		  function(data) {//是否在线
			  if(data.isonline == "null" || data.isonline == null){
				  return "";
			  }else if(data.isonline == "1"){
			  	  return "在线";
			  }else{
			  	  return "未在线";
			  }
		  },
		  function(data) {//创建日期
			  if(data.createdate == "null" || data.createdate == null){
				  return "";
			  }else{
				  return formatDate(data.createdate);
			  }	  
		  },
		  function(data) {//小灵通
			  if(data.pas == "null" || data.pas == null){
				  return "";
			  }else{
				  return data.pas;
			  }	  
		  },
		   function(data) {//手机
			  if(data.mobile == "null" || data.mobile == null){
				  return "";
			  }else{
				  return data.mobile;
			  }	  
		  },
		   function(data) {//联系电话
			  if(data.telphone == "null" || data.telphone == null){
				  return "";
			  }else{
				  return data.telphone;
			  }	  
		  },
		  function(data) {
			return "<a href=\"#\" onclick=\"openAcluserop('"+data.userid+"');return false;\"><img src=\"../themes/default/images/edit2.gif\" border=\"0\" width=\"15\" height=\"15\" valign=\"bottom\" alt=\"编辑\"></a>";
		  }
		  ];
		  DWRUtil.addRows("orguser_table",data,cellFuncs,{
		  rowCreator:function(options) {
		    return document.createElement("tr");
		  },
		  cellCreator:function(options) {
		    var td = document.createElement("td");
		    var cellNum = options.cellNum;
		    td.align = "center";
			//td.height = "28";
		    return td;
		  },
		  escapeHtml:false
		  });
		  /**分页-----------------------------------------------------*/
		  var func_ = "loadAcluserRelaAclorgList(page,10)";
		  var htmlStr = showPage(pageObject,func_);//对象和方法
		  DWRUtil.setValue("orguser_page_div",htmlStr,{escapeHtml:false});
		  addTableRolloverEffect('table_orguser_query','tableRollOverEffect1','tableRowClickEffect1');
		  initSortTable('table_orguser_query',Array('','S','S','S','S','S',''));
	});
	
}

//open add or edit or detail page
function openAcluserop(id){
	var url = "acluserop.html?deptid=" + DWRUtil.getValue("orgid") + "&deptname=" + DWRUtil.getValue("orgname");
	if(id){
		url += "&id=" + id + "&pagetype=org";
	}
	openWin_h(url,"800","500");
}

/**
* @todo：删除用户
* @date：2008-12-16
* @author：wangchen
*/
function setUserStatus(){
	var userIds = getCheckValuesByName("orguserchkbox");
	if(userIds == ""){
		alert("请至少要选择一条删除的用户记录！");
		return false;
	}
	if(confirm("你确定删除用户吗？")){
		DWRUtil.useLoadingMessage("正在删除用户数据...");
		ConfigAction.setUserStatus(userIds,"3",function(data){
			alert(data);
			loadAcluserRelaAclorgList();
		});
	}
}

//@see next js function is for org common function 8:58 2008-2-26

/**set org detail div 11:33 2008-2-25*/
function setOrgdetail(/*String*/str){
	$("div_org_detail").innerHTML = str;
}

/**Role Object15:36 2008-2-25*/
function JSOrgBean(){
	this.id = "";
	this.deptname = "";
	this.deptcode = "";
	this.areacode = "";
	this.depttype = "";
	this.deptorder = "";
	this.parentid = "";
	this.delflag = "";
	this.remark = "";
	this.extend1 = "";
	this.extend2 = "";
}

/**org rela User search Object 10:06 2008-2-26*/
function JSOrgRelaUserSearchBean(){	
	this.account = "";
	this.realname = "";
	this.orgid = "";
}

/**展示详情页面 15:36 2008-2-25*/
function setOrgDetailHtml(){
	$("orgop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">组织信息";
	$("op_button").innerHTML = $("div_hidden_detail").innerHTML;
	setInputReadOnly("deptname");
	setInputReadOnly("deptcode");
	setInputReadOnly("areacode");
	setInputReadOnly("depttype");
	setInputReadOnly("deptorder");
	setInputReadOnly("remark");
}

/**展示编辑页面 15:37 2008-2-25*/
function setOrgEditHtml(){
	$("orgop_title").innerHTML = "<img src=\"../themes/default/images/Contacts.gif\" width=\"16\" height=\"16\">编辑组织";
	$("op_button").innerHTML = $("div_hidden_edit").innerHTML;
	setInputReadOnly("deptname","false");
	setInputReadOnly("deptcode","false");
	setInputReadOnly("areacode","false");
	setInputReadOnly("depttype","false");
	setInputReadOnly("deptorder","false");
	setInputReadOnly("remark","false");
}